package net.lzzy.service;

import net.lzzy.entity.Comment;
import net.lzzy.entity.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service

public class CommentService {
    @Autowired
    CommentRepository commentRepository;
//    public void SelectCommentById(Integer id){
//        Optional<Comment> comment = commentRepository.findById(id);
//        if(comment.isPresent())
//            System.out.println(comment.get());
//        else
//            System.out.println("没有");
//    }
//    public List<Comment> SelectAllSortByAId(){
//        return commentRepository.findAll(Sort.by(Sort.Direction.DESC,"aId"));
//    }
//    public List<Comment> SelectAllpage(int page, int size){
//        Pageable pageable = PageRequest.of(page,size);
//        return commentRepository.findAll(pageable).getContent();
//    }
    public Comment findById(Integer id){
        return commentRepository.findById(id).get();
    }
    public List<Comment> findAll(){
        return commentRepository.findAll();
    }
    public List<Comment> findAllSortByAID(){
        return  commentRepository.findAll(Sort.by(Sort.Direction.DESC,"aId"));
    }
    public List<Comment> findAllByAuthor2(String author){
        Comment comment = new Comment();
        comment.setAuthor(author);
        ExampleMatcher matcher = ExampleMatcher.matching().withMatcher();

    }
    public List<Comment> findAllPage(int page, int size){
        Pageable pageable = PageRequest.of(page,size);
        return commentRepository.findAll(pageable).getContent();
    }
    public long count(){
        return commentRepository.count();
    }
    public Boolean exsitById(Integer id){
        return commentRepository.existsById(id);
    }
    public void deleteComentById(Integer id){
        if (exsitById(id))
            commentRepository.deleteById(id);
    }
}
